// Author: wsfuyibing <682805@qq.com>
// Date: 2025-06-12

package services

import (
	"context"
	"gitee.com/go-libs/db-xorm/db"
	"server/app/errs"
	"server/app/http/models"
)

// ListByIdList
// 按ID列表查询列表.
func (o *TicketService) ListByIdList(ctx context.Context, conditions []int64) (list []*models.Ticket, err error) {
	var sess *db.Session

	// 1. 获取连接.
	if sess, err = o.Dao.Slaver(ctx); err != nil {
		err = errs.ErrDatabaseQuery
		return
	}

	// 2. 主键查询.
	if err = sess.In("id", conditions).Find(&list); err != nil {
		err = errs.ErrDatabaseQuery
	}
	return
}

// MapByIdList
// 按ID列表查询列表.
func (o *TicketService) MapByIdList(ctx context.Context, conditions []int64) (mapper map[int64]*models.Ticket, err error) {
	var list []*models.Ticket
	if list, err = o.ListByIdList(ctx, conditions); err == nil {
		mapper = make(map[int64]*models.Ticket)
		for _, v := range list {
			mapper[v.Id] = v
		}
	}
	return
}

// Paging
// 渔票分页.
func (o *TicketService) Paging(ctx context.Context, data map[string]any, page, size int) (list []*models.Ticket, total int64, err error) {
	var sess *db.Session
	if sess, err = o.Dao.Slaver(ctx); err == nil {
		if total, err = sess.Where(data).Count(models.Ticket{}); err == nil && total > 0 {
			err = sess.Where(data).OrderBy("created_at DESC").Limit(size, (page-1)*size).Find(&list)
		}
	}
	return
}
